package com.xy.server.service;

import com.xy.server.database.FaceMapper;
import com.xy.server.util.ImageUtil;
import com.xy.server.util.ServerUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.RescaleOp;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MosaicTest {

    private static String ResPath = "/Users/agan/IdeaProjects/apache-tomcat-8.5.23-xy/webapps/res/";

    public static boolean mosaicTemplate() throws Exception {

        int size = 25;

        File file = new File(ResPath+"mosaic4k.jpg");
        if (!file.isFile()) {
            throw new Exception("ImageDeal>>>" + file + " 不是一个图片文件!");
        }
        BufferedImage bi = ImageIO.read(file); // 读取该图片

        if (bi.getWidth() < size || bi.getHeight() < size || size <= 0) { // 马赛克格尺寸太大或太小
            return false;
        }

        List<BufferedImage> iconList = new ArrayList<>();

        iconList.add(ImageIO.read(new File(ResPath+"photo2_cut__1523103320844.jpg")));
        iconList.add(ImageIO.read(new File(ResPath+"photo2_cut__1523103320858.jpg")));
        iconList.add(ImageIO.read(new File(ResPath+"photo2_cut__1523103320868.jpg")));
        iconList.add(ImageIO.read(new File(ResPath+"photo2_cut__1523103320877.jpg")));
        iconList.add(ImageIO.read(new File(ResPath+"photo2_cut__1523103320886.jpg")));
//        iconList.add(ImageIO.read(new File(ResPath+"1.png")));
//        iconList.add(ImageIO.read(new File(ResPath+"2.jpg")));
//        iconList.add(ImageIO.read(new File(ResPath+"3.jpg")));
//        iconList.add(ImageIO.read(new File(ResPath+"4.jpg")));
        iconList.add(ImageIO.read(new File(ResPath+"2018-04-07_14.50.10_cut__1523083812993.png")));
//        BufferedImage icon = ImageIO.read(fileSmall); // 读取该图片



        BufferedImage spinImage = new BufferedImage(bi.getWidth(),
                bi.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics gs = spinImage.getGraphics();


        int xcount = 0; // 方向绘制个数
        int ycount = 0; // y方向绘制个数
        if (bi.getWidth() % size == 0) {
            xcount = bi.getWidth() / size;
        } else {
            xcount = bi.getWidth() / size + 1;
        }
        if (bi.getHeight() % size == 0) {
            ycount = bi.getHeight() / size;
        } else {
            ycount = bi.getHeight() / size + 1;
        }
        int x = 0;   //坐标
        int y = 0;

        int iconCount = 0;
        int iconTotalCount = 0;

        // 绘制马赛克(绘制矩形并填充颜色)
        for (int i = 0; i < xcount; i++) {
            for (int j = 0; j < ycount; j++) {

                //马赛克矩形格大小
                int mwidth = size;
                int mheight = size;
                if(i==xcount-1){   //横向最后一个比較特殊，可能不够一个size
                    mwidth = bi.getWidth()-x;
                }
                if(j == ycount-1){  //同理
                    mheight =bi.getHeight()-y;
                }
                // 矩形颜色取中心像素点RGB值
                int centerX = x;
                int centerY = y;
                if (mwidth % 2 == 0) {
                    centerX += mwidth / 2;
                } else {
                    centerX += (mwidth - 1) / 2;
                }
                if (mheight % 2 == 0) {
                    centerY += mheight / 2;
                } else {
                    centerY += (mheight - 1) / 2;
                }
                Color color = new Color(bi.getRGB(centerX, centerY));
                //gs.setColor(color);

                int red_count = 0;
                int green_count = 0;
                int blue_count = 0;
                int pixel_count = 0;

                BufferedImage icon =  iconList.get( (int)(Math.random() * 100)% iconList.size());

                /* 调整后的图片的宽度和高度 */

                /* 新生成结果图片 */
                BufferedImage newIcon = new BufferedImage(mwidth, mheight,
                        BufferedImage.TYPE_INT_RGB);

                newIcon.getGraphics().drawImage(
                        icon.getScaledInstance(mwidth, mheight,
                                java.awt.Image.SCALE_SMOOTH), 0, 0, null);

                for (int ty = 0; ty < mheight; ty++)
                {
                    for (int tx = 0; tx < mwidth; tx++)
                    {
                        Color c = new Color(bi.getRGB(tx+x, ty+y));

                        pixel_count++;
                        red_count += c.getRed();
                        green_count += c.getGreen();
                        blue_count += c.getBlue();
                    }
                }


                Color ac = new Color(red_count*1.0f / pixel_count/255,
                        green_count*1.0f / pixel_count/255,
                        blue_count*1.0f / pixel_count/255);



                System.out.println(ac.getRed());
                System.out.println(ac.getGreen());
                for (int ty = 0; ty < mheight; ty++) {
                    for (int tx = 0; tx < mwidth; tx++) {
                        Color ic = new Color(newIcon.getRGB(tx,ty));

//                        Color nc = new Color(
//                                (int)(ac.getRed()*0.5f + ic.getRed()*0.5f),
//                                (int)(ac.getGreen()*0.5f + ic.getGreen()*0.5f),
//                                (int)(ac.getBlue()*0.5f + ic.getBlue()*0.5f),
//                                (int)(ac.getAlpha()*0.5f + ic.getAlpha()*0.5f));

                        Color nc = new Color(
                                (int)(ac.getRed()*0.7f + ic.getRed()*0.3f),
                                (int)(ac.getGreen()*0.7f + ic.getGreen()*0.3f),
                                (int)(ac.getBlue()*0.7f + ic.getBlue()*0.3f),
                                (int)(ac.getAlpha()*0.7f + ic.getAlpha()*0.3f));

//                        Color nc = new Color(
//                                (int)(ac.getRed()*0.6f + ic.getRed()*0.4f),
//                                (int)(ac.getGreen()*0.6f + ic.getGreen()*0.4f),
//                                (int)(ac.getBlue()*0.6f + ic.getBlue()*0.4f),
//                                (int)(ac.getAlpha()*0.6f + ic.getAlpha()*0.4f));

                        newIcon.setRGB(tx,ty,nc.getRGB());
                    }
                }

//                BufferedImage back = new BufferedImage(mwidth, mheight,
//                        BufferedImage.TYPE_INT_RGB);
//                bac
//                Graphics2D g2d = buffImg.createGraphics();
//                int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
//                int waterImgHeight = waterImg.getHeight();// 获取层图的高度
//                // 在图形和图像中实现混合和透明效果
//                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
//                // 绘制
//                g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
//                g2d.dispose();// 释放图形上下文使用的系统资源


                gs.drawImage(newIcon, x, y, size, size, null);

                iconTotalCount++;

                if(ac.getRed() < 10 && ac.getBlue()< 10 && ac.getGreen() < 10){

                }
                else {

                    iconCount++;

                }

                y = y + size;// 计算下一个矩形的y坐标
            }
            y = 0;// 还原y坐标
            x = x + size;// 计算x坐标
        }

        gs.dispose();
        System.out.println("icon total count " + iconTotalCount);
        System.out.println("icon count " + iconCount);

        File sf = new File(ResPath, "gan.jpg");
        ImageIO.write(spinImage, "jpg", sf); // 保存图片



        return true;
    }

    public static void main(String[] args) throws Exception {
       // mosaicTemplate();


        ImageUtil.rotatePhoto("D:/Work/apache-tomcat-7.0.86-xy/webapps/res-img/" ,"me.jpeg",270);
    }
}
